#include <QMenu>

#include "tab_conf.h"

CTabConf::CTabConf(CPropertiesModel* pModel,
		QWidget* pParent) :
	QWidget(pParent),
	mModel(pModel),
	mLayout(this),
	mSrvConfigTree(),
	mSectionCnt(1)
{
	setupUiElements();
	setupActions();

	mSrvConfigTree.setModel(mModel);
	mSrvConfigTree.setSortingEnabled(true);
	mSrvConfigTree.sortByColumn(0, Qt::DescendingOrder);
	mSrvConfigTree.resizeColumnToContents(0);
}

CTabConf::~CTabConf()
{
}

void CTabConf::loadFromFile(const QString& pFilename)
{
	mModel->loadProperties(pFilename);
	mSrvConfigTree.sortByColumn(0, Qt::AscendingOrder);
}

void CTabConf::saveToFile(const QString& pFilename)
{
	mModel->saveProperties(pFilename);
}

void CTabConf::contextMenuRequested(const QPoint& pPoint)
{
	if (mModel->isReadOnly())
	{
		return;
	}

	QMenu* lMenu = new QMenu;
	if (mSrvConfigTree.currentIndex().isValid())
	{
		if (mModel->parentIsRoot(mSrvConfigTree.selectionModel()->currentIndex()))
		{
			lMenu->addAction(tr("New &Section"), this, SLOT(newSectionRequested()));
		}
		else if (mModel->parentIsRoot(mSrvConfigTree.selectionModel()->currentIndex().parent()))
		{
			lMenu->addAction(tr("New &Key"), this, SLOT(newKeyRequested()));
		}
		lMenu->addAction(tr("&Delete"), this, SLOT(deleteRequested()));
	}
	else
	{
		lMenu->addAction(tr("New &Section"), this, SLOT(newSectionRequested()));
	}
	lMenu->exec(this->mapToGlobal(pPoint));
}

void CTabConf::newSectionRequested()
{
	mModel->insertSection(QVariant("New Section " + QString::number(mSectionCnt, 10)));
	mSectionCnt++;
}

void CTabConf::newKeyRequested()
{
	mSrvConfigTree.selectionModel()->setCurrentIndex(
			mModel->insertKey(
				mSrvConfigTree.selectionModel()->currentIndex(),
				QVariant("New Variable"), 
				QVariant("<value>")), 
			QItemSelectionModel::ClearAndSelect);
	mSrvConfigTree.resizeColumnToContents(0);
}

void CTabConf::deleteRequested()
{
	QModelIndex lIndex = mSrvConfigTree.selectionModel()->currentIndex();
	mModel->removeRow(lIndex.row(), lIndex.parent());
}

void CTabConf::configRequested()
{
	mModel->dropProperties();
}

void CTabConf::startRequested()
{
	mModel->setReadOnly(true);
}

void CTabConf::clientDisconnected()
{
	mModel->setReadOnly(false);
}

void CTabConf::sendConfigRequested(CClientConnection* pConnection)
{
	mModel->sendConfig(pConnection);
}

void CTabConf::dataChanged(const QModelIndex& pTopLeft, const QModelIndex& pBottomRight)
{
	mSrvConfigTree.resizeColumnToContents(0);
}

void CTabConf::treeExpanded(const QModelIndex& pIndex)
{
	mSrvConfigTree.resizeColumnToContents(pIndex.column());
}

void CTabConf::treeCollapsed(const QModelIndex& pIndex)
{
	mSrvConfigTree.resizeColumnToContents(pIndex.column());
}

void CTabConf::modelSorted()
{
	mSrvConfigTree.resizeColumnToContents(0);
}

void CTabConf::configReceivedEnd()
{
	mSrvConfigTree.sortByColumn(0, Qt::AscendingOrder);
}

void CTabConf::setupUiElements()
{
	mSrvConfigTree.setContextMenuPolicy(Qt::CustomContextMenu);

	mLayout.addWidget(&mSrvConfigTree);
	mSrvConfigTree.setStyleSheet(" \
 QTreeView::branch:has-siblings:!adjoins-item { \
     border-image: url(icons/vline.png) 0; \
 } \
 \
 QTreeView::branch:has-siblings:adjoins-item { \
     border-image: url(icons/branch-more.png) 0; \
 } \
 \
 QTreeView::branch:!has-children:!has-siblings:adjoins-item { \
     border-image: url(icons/branch-end.png) 0; \
 } \
 \
 QTreeView::branch:has-children:!has-siblings:closed, \
 QTreeView::branch:closed:has-children:has-siblings { \
         border-image: none; \
         image: url(icons/branch-closed.png); \
 } \
 \
 QTreeView::branch:open:has-children:!has-siblings, \
 QTreeView::branch:open:has-children:has-siblings  { \
         border-image: none; \
         image: url(icons/branch-open.png); \
 } \
		");
	connect(mModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
			this, SLOT(dataChanged(const QModelIndex&, const QModelIndex&)));
}

void CTabConf::setupActions()
{
	connect(&mSrvConfigTree, SIGNAL(customContextMenuRequested(const QPoint& )), 
			this, SLOT(contextMenuRequested(const QPoint& )));
	connect(&mSrvConfigTree, SIGNAL(expanded(const QModelIndex& )), 
			this, SLOT(treeExpanded(const QModelIndex& )));
	connect(&mSrvConfigTree, SIGNAL(collapsed(const QModelIndex& )), 
			this, SLOT(treeCollapsed(const QModelIndex& )));
	connect(mModel, SIGNAL(modelSorted()), 
			this, SLOT(modelSorted()));
}

